/**
 * Create by richliu1023
 * @date 2016-06-23
 * @email richliu1023@gmail.com
 * @github https://github.com/RichLiu1023
 * @description a simple explanation
 */
class ByteArrayUtil {

	private static _buffer:egret.ByteArray = new egret.ByteArray();

	public constructor() {
	}

	public static writeUTFBytesWithFixedLength( byteArray:egret.ByteArray,
												data:string,
												maxSize:number ):egret.ByteArray {
		this._buffer.clear();
		this._buffer.writeUTFBytes( data );
		var len = this._buffer.length;
		byteArray.writeBytes( this._buffer, 0, Math.min( len, maxSize ) );
		this._buffer.clear();
		var dummy = maxSize - len;
		if ( dummy > 0 ) {
			for ( var i = 0; i < dummy; i++ )
				byteArray.writeByte( 0 );
		}
		return byteArray;
	}

	public static byteArrayToInt64( b:egret.ByteArray, pos:number ):number {
		this._buffer.clear();
		if ( b != null && b.length >= (pos + 8) ) {
			this._buffer.writeBytes( b, pos, 8 );
			this._buffer.position = 0;
			var num:number = ((this._buffer.readByte() & 0xFF) << 56) |
				((this._buffer.readByte() & 0xFF) << 48) |
				((this._buffer.readByte() & 0xFF) << 40) |
				((this._buffer.readByte() & 0xFF) << 32) |
				((this._buffer.readByte() & 0xFF) << 24) |
				((this._buffer.readByte() & 0xFF) << 16) |
				((this._buffer.readByte() & 0xFF) << 8) |
				(this._buffer.readByte() & 0xFF);
			this._buffer.clear();
			return num;
		}
	}
}